// 
//  Profile.m
//  Bango
//

#import "Profile.h"

// IMF Imports
#import "Event.h"
#import "BangoFieldNames.h"

@implementation Profile 

@dynamic device;
@dynamic applicationKey;
@dynamic applicationChannel;
@dynamic applicationType;
@dynamic locationAltitude;
@dynamic ipAddress;
@dynamic userId;
@dynamic created;
@dynamic operator;
@dynamic brand;
@dynamic locationLatitude;
@dynamic version;
@dynamic locationAccuracy;
@dynamic sim;
@dynamic applicationVersion;
@dynamic sdkVersion;
@dynamic locationLongitude;
@dynamic networkType;
@dynamic model;
@dynamic product;
@dynamic connectionType;
@dynamic sdkType;
@dynamic isRoaming;
@dynamic sessionRestartTime;
@dynamic buildId;
@dynamic events;

// Non-Auto Generated IMF Code
// todo check for 0 values for NSNumber fields
- (id)proxyForJson {
	
	NSMutableArray *eventsArray = [[NSMutableArray alloc] initWithArray:[self.events allObjects]];
	
	//NSLog(@"eventsArray %@", eventsArray);
	
	// we can just return this
	NSMutableDictionary *profileDictionary = [[[NSMutableDictionary alloc] init] autorelease];
		
	if(self.applicationKey != nil) {
		[profileDictionary setObject:self.applicationKey forKey:POST_APPLICATIONID];
	}
	
	if(self.userId != nil) {
		[profileDictionary setObject:self.userId forKey:POST_USERID];
	}
	
	// SDKType
	if(self.sdkType != nil) {
		[profileDictionary setObject:self.sdkType forKey:POST_SDKTYPE];
	}
	
	// SDKVersion
	if(self.sdkVersion != nil) {
		[profileDictionary setObject:self.sdkVersion forKey:POST_SDKVERSION];
	}
	
	
	if(self.sessionRestartTime != nil) {
		[profileDictionary setObject:[self.sessionRestartTime stringValue] forKey:POST_SESSIONRESTARTTIME];
	}
	
	if(self.applicationChannel != nil) {
		[profileDictionary setObject:self.applicationVersion forKey:POST_APPLICATIONCHANNEL];
	}
	
	if(self.applicationType != nil) {
		[profileDictionary setObject:self.applicationVersion forKey:POST_APPLICATIONTYPE];
	}
	
	if(self.applicationVersion != nil) {
		[profileDictionary setObject:self.applicationVersion forKey:POST_APPLICATIONVERSION];
	}
	
	if(self.ipAddress != nil) {
		[profileDictionary setObject:self.applicationVersion forKey:POST_IP];
	}
	
	if(self.operator != nil) {
		[profileDictionary setObject:self.operator forKey:POST_OPERATORNAME];
	}
	
	if(self.brand != nil) {
		[profileDictionary setObject:self.brand forKey:POST_DEVICEBRAND];
	}
	
    if(self.device != nil) {
		[profileDictionary setObject:self.device forKey:POST_DEVICEMAKE];
	}
	
	if(self.model != nil) {
		[profileDictionary setObject:self.model forKey:POST_DEVICEMODEL];
	}
	
	if(self.version != nil) {
		[profileDictionary setObject:self.version forKey:POST_DEVICEVERSION];
	}
	
	// is mobile device
	bool canMakeCalls = [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"tel://"]];
	[profileDictionary setObject:[NSNumber numberWithBool:canMakeCalls] forKey:POST_DEVICEISMOBILE];
	
		
	// operating system name + version
	NSString *osName = [UIDevice currentDevice].systemName;
	NSString *osVersion = [UIDevice currentDevice].systemVersion;
	
	[profileDictionary setObject:osName forKey:POST_OPERATINGSYSTEM];
	[profileDictionary setObject:osVersion forKey:POST_OPERATINGSYSTEMVERSION];
	
	if(self.locationLatitude != nil) {
		[profileDictionary setObject:[self.locationLatitude stringValue] forKey:POST_LOCATIONLATITUDE];
	}
	
	if(self.locationLongitude != nil) {
		[profileDictionary setObject:[self.locationLongitude stringValue] forKey:POST_LOCATIONLONGITUDE];
	}
	
	if(self.locationAltitude != nil) {
		[profileDictionary setObject:[self.locationAltitude stringValue] forKey:POST_LOCATIONALTITUDE];
	}
	
	if(self.locationAccuracy != nil) {
		[profileDictionary setObject:[self.locationAccuracy stringValue] forKey:POST_LOCATIONACCURACY];
	}
	
	if(self.connectionType != nil) {
		[profileDictionary setObject:self.connectionType forKey:POST_CONNECTIONTYPE];
	}
	
	if(self.isRoaming != nil) {
		[profileDictionary setObject:[self.isRoaming stringValue] forKey:POST_ISROAMING];
	}
	
	if([eventsArray count] > 0) {
		[profileDictionary setObject:eventsArray forKey:POST_EVENTS];
	}
	
	[eventsArray release];

    return profileDictionary;
}


@end
